const { src, dest, watch, series, parallel } = require('gulp');
const del = require('del');
const uglify = require('gulp-uglify');
const rename = require('gulp-rename');
const babel = require('gulp-babel');
// const imagemin = require('gulp-imagemin');
const minifyCSS = require('gulp-minify-css');
const goweb = require('./gulp/goweb')
const goless = require('./gulp/goless')
const goscss = require('./gulp/goscss')

function msg(cb, type) {
    console.log(type, '<<<=== 所有事件执行完毕 ===>>>');
    cb()
}

// 清除 dist 下的文件
function clear() {
    return del('dist/**/*')
}

// 复制 html 文件
function copyhtml() {
    return src('src/*.html').pipe(dest('dist/'));
}

// 压缩css
function minicss() {
    return src('dist/css/**/*.css')
        .pipe(minifyCSS())
        .pipe(dest('dist/css'));
}

// babel解析
function babelc() {
    return src('src/**/*.js')
        .pipe(babel({ presets: ['@babel/env'] }))
        .pipe(dest('dist/js'));
}
// 压缩 js 并修改后缀名
function minijs() {
    return src('dist/js/**/*.js')
        .pipe(uglify())
        // 把源文件，改为`.mini.js` 移动到 dist
        .pipe(rename({ extname: '.min.js' }))
        .pipe(dest('dist/js'));
}

// 压缩图片
const miniimg = function () {
    return src('src/**/*.jpg')
        .pipe(imagemin({ progressive: true }))
        .pipe(dest('dist/img'));
}

// 自动监控热更新
function dev() {
    return watch('src/**/*', series(clear, copyhtml, parallel(goless, babelc)));
}


exports.miniimg = miniimg

exports.clear = clear
exports.babelc = babelc
exports.minijs = minijs

exports.goweb = goweb
exports.goless = goless
exports.goscss = goscss
exports.minicss = minicss
exports.dev = dev
// 打包项目
exports.build = series(clear, copyhtml, parallel(series(goless, minicss), series(babelc, minijs)), msg)
exports.default = msg